<template>
	<div v-if="key == this.routePath.defaultPath">
		<el-form :inline="true" :model="searchForm">
			<el-form-item label="商品sku编号">
				<el-input placeholder="商品sku编号" v-model="searchForm.goodsSkuCode"></el-input>
			</el-form-item>
			<el-form-item label="推荐类别">
				<el-select v-model="searchForm.isMeddle" class="input-width" placeholder="全部" clearable>
					<el-option v-for="item in meddleOptions"
						:key="item.value"
						:label="item.label"
						:value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="推荐状态">
				<el-select v-model="searchForm.goodsStatus" class="input-width" placeholder="全部" clearable>
					<el-option v-for="item in goodsStatusOptions"
						:key="item.value"
						:label="item.label"
						:value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
				<el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleCreate">添加</el-button>
			</el-form-item>
		</el-form>
		<!-- 列表 -->
		<el-table :data="dataList" height="calc(100VH - 270px)" highlight-current-row border style="width: 100%" v-loading="loading">
			<el-table-column label="序号" type="index" align="center" width="50"></el-table-column>
			<el-table-column label="商品名称" prop="goodsName" align="center" show-overflow-tooltip></el-table-column>
			<el-table-column label="排序" prop="goodsSort" align="center"></el-table-column>
			<el-table-column label="推荐类别" prop="isMeddle" align="center" >
				<template slot-scope="scope">
					{{scope.row.isMeddle == '1' ? '系统默认':'运营干预'}}
				</template>
			</el-table-column>
			<el-table-column label="创建时间" prop="createTime" align="center" ></el-table-column>
			<el-table-column label="推荐状态" prop="goodsStatus"  align="center">
				<template slot-scope="scope">
					<el-switch
					@change="handleStatusChange(scope.$index, scope.row)"
					:active-value="1"
					:inactive-value="2"
					v-model="scope.row.goodsStatus">
					</el-switch>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center"  fixed="right">
				<template slot-scope="scope">
					<el-button v-if="permission_sort" type="text" size="small" @click="handleDelete(scope.row.id)">修改排序</el-button>
					<el-button v-if="permission_delete" type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页 -->
		<div v-show="!loading" class="pagination-container">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageForm.pageNum"
			:page-sizes="pageForm.pageSizeSelect" :page-size="pageForm.pageSize" :total="pageForm.totalData" layout="total, sizes, prev, pager, next, jumper"
			:prev-text="pageForm.prevText" :next-text="pageForm.nextText">
			</el-pagination>
		</div>
		<el-divider></el-divider>
		<div align="center">
			<el-button type="info" @click="back">返回</el-button>
		</div>
	</div>
	<div v-else>
		<router-view :key="key"></router-view>
	</div>
</template>

<script>
	import { mapGetters } from 'vuex'
	import { findPrefectureGoodsList,modifyPrefectureGoodsStatus,deletePrefectureGoods } from '@/api/activity/prefecture'
	export default {
		name: 'prefectureGoods',
		data() {
			return {
				// 默认加载弹出层
				loading: true,
				// 数据列表el-table
				dataList: [],
				// 权限按钮控制
				permission_add: false,
				permission_delete: false,
				permission_sort: false,
				// 搜索条件
				searchForm: {
					prefectureCode: '',
					goodsSkuCode: '',
					isMeddle: '',
					goodsStatus: '',
					pageNum: 1,
					pageSize: 10
				},
				//路由地址
				routePath: {
					defaultPath: '/activity/prefecture/maintain',
					addGoodsPath: '/activity/prefecture/maintain/add',
				},
				//分页相关
				pageForm: {
					pageSizeSelect: [10, 20, 30, 50], //每页显示个数选择器的选项设置
					totalData: 0, //总条数
					pageSize: 10, //每页显示条目个数
					pageNum: 1, //当前页
					prevText: '上一页', //替代图标显示的上一页文字
					nextText: '下一页' //替代图标显示的下一页文字
				},
				meddleOptions: [
					{
						label: '系统默认',
						value: 1
					},
					{
						label: '运营干预',
						value: 2
					},
				],
				goodsStatusOptions: [
					{
						label: '推荐中',
						value: 1
					},
					{
						label: '未推荐',
						value: 2
					},
				],
				
				
			}
		},
		computed: {
			key() {
				return this.$route.path
			},
			...mapGetters([
				'elementList'
			])
		},
		components: {},
		created() {
			if (this.$route.path === this.routePath.defaultPath) {
				// 初始化获取数据列表
				this.searchForm.prefectureCode = this.$route.query.prefectureCode
				this.getDateList()
				//初始化按钮权限
				this.permission_add = this.elementList['activity:prefecturegoods:add']
				this.permission_delete = this.elementList['activity:prefecturegoods:delete']
				this.permission_sort = this.elementList['activity:prefecturegoods:modifysort']
			}
		},
		methods: {
			getDateList() {
				this.loading = true
				findPrefectureGoodsList(this.searchForm).then(res => {
					this.dataList = res.data.responseData
					this.pageForm.totalData = parseInt(res.data.total)
				})
				this.loading = false
			},
			handleSearch() {

			},
			handleDelete(val) {
				this.$confirm('是否要移除此商品?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					deletePrefectureGoods(val).then(() => {
						this.$notify({
							title: '成功',
							message: '删除成功',
							type: 'success',
							duration: 2000,
							onClose: this.getDateList()
						});
					}).catch((err) => {
						this.$message.error(err.data.responseMessage);
					});
				});
			},
			handleSizeChange(val) {
				this.searchForm.pageSize = val
				this.getDateList()
			},
			handleCurrentChange(val) {
				this.searchForm.pageNum = val
				this.getDateList()
			},
			handleStatusChange(index, row){
				this.$confirm('是否要修改该状态?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					let data = {
						id: row.id,
						goodsStatus: row.goodsStatus
					}
					modifyPrefectureGoodsStatus(data).then(() => {
						this.$message({
							type: 'success',
							message: '修改成功!'
						});
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '取消修改'
					});
					this.getDateList();
				});
			},
			handleCreate(){
				this.$router.push({path: this.routePath.addGoodsPath,query: {prefectureCode: this.searchForm.prefectureCode}});
			},
			back() {
				this.$router.go(-1)
			},
		}
	}
</script>
